package com.kangyi.app.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.app.mapper.FeedbackMapper;
import com.kangyi.app.mapper.UserComplaintImgMapper;
import com.kangyi.app.service.FeedbackService;
import com.kangyi.dto.SubmitDTO;
import com.kangyi.dto.UserComplaintImgDTO;
import com.kangyi.dto.UserPageDTO;
import com.kangyi.entity.UserComplaint;
import com.kangyi.entity.UserComplaintImg;
import com.kangyi.result.PageResult;
import com.kangyi.utils.AuthUtil;
import com.kangyi.vo.FeedBackVO;
import com.kangyi.vo.UserComplaintImgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private UserComplaintImgMapper userComplaintImgMapper;

    @Override
    public void submit(SubmitDTO submitDTO) {
        AuthUtil.validateUserPermission(submitDTO.getUserId());
        UserComplaint build = UserComplaint.builder()
                .userId(submitDTO.getUserId())
                .theme(submitDTO.getTheme())
                .content(submitDTO.getContent())
                .name(submitDTO.getName())
                .phone(submitDTO.getPhone())
                .status(UserComplaint.PENDING)
                .build();
        feedbackMapper.submit(build);
        Integer id = build.getId();
        for (UserComplaintImgDTO userComplaintImgDTO : submitDTO.getUserComplaintImgDTOList()) {
            UserComplaintImg userComplaintImg = UserComplaintImg.builder()
                    .userComplaintId(id)
                    .image(userComplaintImgDTO.getImage())
                    .sort(userComplaintImgDTO.getSort())
                    .build();
            userComplaintImgMapper.add(userComplaintImg);
        }

    }

    @Override
    public PageResult list(UserPageDTO userPageDTO) {
        PageHelper.startPage(userPageDTO.getPage(),userPageDTO.getPageSize());
        Page<FeedBackVO> page = feedbackMapper.list(userPageDTO);
        long total = page.getTotal();
        List<FeedBackVO> result = page.getResult();
        for (FeedBackVO vo : result) {
            List<UserComplaintImgVO> userComplaintImgVOList = userComplaintImgMapper.getById(vo.getId());
            vo.setUserComplaintImgVOList(userComplaintImgVOList);
        }
        return new PageResult(total,result);
    }
}
